#ifndef TCP_CONNECT_H
#define TCP_CONNECT_H

#include "SocketIO.h"
#include "InetAddress.h"
#include <memory>
#include <string>
#include <sstream>
#include <string.h>
#include <functional>

using std::function;
using std::ostringstream;
using std::shared_ptr;

class TcpConnect : public std::enable_shared_from_this<TcpConnect>
{
    using TcpConnectPtr = shared_ptr<TcpConnect>;
    using Callback = function<void(TcpConnectPtr)>;

private:
    int _cltFd;
    SocketIO _io;
    InetAddress _local;
    InetAddress _peer;

    Callback _handleConnectCb;
    Callback _handleMessageCb;
    Callback _handleClosedCb;

private:
    InetAddress getLocal() const;
    InetAddress getPeer() const;

public:
    TcpConnect(int fd);
    TcpConnect(const TcpConnect &conn);

    void write(const char *str);
    string read();
    string toString() const;

    void setConnectCallback(Callback cb);
    void setHandleMessageCallback(Callback cb);
    void setCloseCallback(Callback cb);

    void handleConnect();
    void handleMessage();
    void handleClose();

    string peerIp() const;
    unsigned short peerPort() const;
};

#endif